/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2023. All rights reserved.
 */

package dao

import (
	"fmt"
	"prim-server/common"
	"prim-server/model"

	"github.com/astaxie/beego/orm"
	"github.com/pkg/errors"
	"github.com/spf13/cast"
)

type ProductionDao struct {
	BaseDao
}

type IProductionDao interface {
	SaveProduction(production *model.Production) error
	DeleteProduction(productionId string) error
	UpdateProduction(production *model.Production) error
	GetProductions(queryParameter *common.QueryProductionDetail) (productions []*model.Production, totalCount int, err error)
}

func GetProductionDao() IProductionDao {
	dao := &ProductionDao{}
	dao.Init()
	return dao
}

func (pd *ProductionDao) SaveProduction(production *model.Production) error {
	if production == nil {
		return nil
	}
	_, err := pd.O.Insert(production)
	if err != nil {
		return errors.Errorf("SaveProduction error: %v, productionId: %s", err, production.ProductionId)
	}
	return nil
}

func (pd *ProductionDao) DeleteProduction(productionId string) error {
	if len(productionId) == 0 {
		return nil
	}
	production := new(model.Production)
	production.ProductionId = productionId
	_, err := pd.O.Delete(production, "production_id")
	if err != nil {
		return errors.Errorf("DeleteProduction error: %v, productionId: %s", err, production.ProductionId)
	}
	return nil
}
func (pd *ProductionDao) UpdateProduction(production *model.Production) error {
	if production == nil {
		return nil
	}
	_, err := pd.O.Update(production)
	if err != nil {
		return errors.Errorf("UpdateProduction error: %v, productionId: %s", err, production.ProductionId)
	}
	return nil
}

func (pd *ProductionDao) GetProductions(queryParameter *common.QueryProductionDetail) (productions []*model.Production, totalCount int, err error) {
	if queryParameter == nil {
		return nil, 0, nil
	}
	recode := pd.O.QueryTable(&model.Production{})
	finalCondition := orm.NewCondition()
	if queryParameter.ProductionId != nil && len(queryParameter.ProductionId) > 0 {
		finalCondition = finalCondition.And("production_id__in", queryParameter.ProductionId)
	}
	if queryParameter.ProductionType != nil && len(queryParameter.ProductionType) > 0 {
		finalCondition = finalCondition.And("production_type__in", queryParameter.ProductionType)
	}
	if queryParameter.ProductionName != nil && len(queryParameter.ProductionName) > 0 {
		finalCondition = finalCondition.And("production_name__in", queryParameter.ProductionName)
	}
	recode = recode.SetCond(finalCondition)
	totalCount64, err := recode.Count()
	if err != nil {
		return nil, 0, errors.Errorf("GetProductions error: %v, productionId: %s", err, fmt.Sprint(queryParameter.ProductionId))
	}
	totalCount = cast.ToInt(totalCount64)
	if queryParameter.Page != nil && queryParameter.PageSize != nil {
		offset, pageSize := pd.GetOffsetAndPageSize(queryParameter.Page, queryParameter.PageSize)
		_, err = recode.Offset(offset).Limit(pageSize).All(&productions)
	} else {
		_, err = recode.All(&productions)
	}
	if err != nil {
		if err == orm.ErrNoRows {
			//not found
			return nil, 0, nil
		}
		//found error
		return nil, 0, errors.Errorf("GetProductions error: %v, productionId: %s", err, fmt.Sprint(queryParameter.ProductionId))
	}
	return productions, totalCount, nil
}
